import sys, os, random
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, 
    QGridLayout, QHBoxLayout, QVBoxLayout, QGroupBox,
    QPushButton, QLabel, QComboBox, QSizePolicy
)
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtCore import Qt
from setting import *
from styles import *
class BanWidget(QWidget):
    """
    使用原生 QComboBox + QLabel 显示 BAN 角色图片。
    ban_options: 某文件夹下的角色名字列表
    ban_folder : 文件夹名称，如 "survivor_ban" 或 "hunter_ban"
    """
    def __init__(self, ban_options, ban_folder, parent=None):
        super().__init__(parent)
        self.ban_options = ban_options
        self.ban_folder = ban_folder
        self.current_ban_name = None

        layout = QVBoxLayout(self)
        layout.setSpacing(5)

        self.combo = QComboBox()
        self.combo.addItem("（不BAN）")
        for name in self.ban_options:
            self.combo.addItem(name)
        self.combo.currentIndexChanged.connect(self.on_ban_selected)
        layout.addWidget(self.combo)

        self.ban_image = QLabel()
        self.ban_image.setFixedSize(BAN_IMG_SIZE, BAN_IMG_SIZE)
        self.ban_image.setStyleSheet(global_label_style)
        self.ban_image.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.ban_image, alignment=Qt.AlignCenter)

    def on_ban_selected(self, idx):
        if idx <= 0:
            self.current_ban_name = None
            self.ban_image.clear()
            self.ban_image.setStyleSheet(global_label_style)
            return
        name = self.combo.currentText()
        self.current_ban_name = name
        pix = self.load_ban_image(name)
        self.ban_image.setPixmap(pix)
        if pix.isNull():
            self.ban_image.setStyleSheet(global_label_style)
        else:
            self.ban_image.setStyleSheet("")

    def load_ban_image(self, ban_name):
        path = os.path.join("images", self.ban_folder, f"{ban_name}.jpg")
        pix = QPixmap(path)
        if pix.isNull():
            return QPixmap()
        return pix.scaled(BAN_IMG_SIZE, BAN_IMG_SIZE, Qt.KeepAspectRatio, Qt.SmoothTransformation)

    def reset(self):
        self.combo.setCurrentIndex(0)
        self.ban_image.clear()
        self.ban_image.setStyleSheet(global_label_style)
